﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Net.Mail;
using System.Text;

using AKMII.DMRA.Common;
using System.IO;

namespace AKMII.DMRA.Business.Service
{
    public class SmtpMailService
    {
        private string smtpServer;

        private string mailAccount;

        private string password;

        public SmtpMailService(string smtpServer, string mailAccount, string password)
        {
            this.smtpServer = smtpServer;
            this.mailAccount = mailAccount;
            this.password = password;
        }

        public void Send(Mail body)
        {
            SmtpClient client = new SmtpClient(smtpServer, 25);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(mailAccount, password);
            bool isExecute = true;
            int retryCount = 0;
            MailMessage message = null;

            while (isExecute)
            {
                try
                {
                    message = ConstructMailMessage(body);
                    client.Send(message);
                    isExecute = false;

                    if (body.Recipients != null)
                    {
                        foreach (var recipient in body.Recipients)
                        {
                            Logger.Info("Send mail to " + recipient);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception ocurrs in sending mail", ex);
                    retryCount++;

                    if (retryCount > 3)
                    {
                        isExecute = false;
                    }
                }
                finally
                {
                    if (message != null)
                    {
                        message.Dispose();
                        message = null;
                    }
                }
            }
        }

        #region Prvivate Methods

        private MailMessage ConstructMailMessage(Mail body)
        {
            MailMessage message = new MailMessage();

            message.From = new MailAddress(mailAccount);
            if (body.Recipients != null)
            {
                foreach (var address in body.Recipients)
                {
                    message.To.Add(new MailAddress(address));
                }
            }
            message.Subject = body.Subject;
            message.Body = body.Content;

            if (!string.IsNullOrEmpty(body.AttachmentFile))
            {
                if (File.Exists(body.AttachmentFile))
                {
                    message.Attachments.Add(new Attachment(body.AttachmentFile));
                }
            }

            message.IsBodyHtml = true;

            return message;
        }

        #endregion
    }
}
